package DAL;

import Entities.TblRoles;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class RoleDAL {

    private ResultSet objResult;
    public List<TblRoles> list;

    public RoleDAL() {
    }

    public boolean add(TblRoles role) {
        int result = 0;
        String[] params = new String[2];
        params[0] = role.getRoleName();
        params[1] = role.getDescription();

        try {
            result = SQLHelper.executeUpdate("procInsertRole", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblRoles role) {
        int result = 0;
        String[] params = new String[3];
        params[0] = String.valueOf(role.getRoleId());
        params[1] = role.getRoleName();
        params[2] = role.getDescription();

        try {
            result = SQLHelper.executeUpdate("procUpdateRole", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblRoles role) {
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(role.getRoleId());

        try {
            result = SQLHelper.executeUpdate("procDeleteRole", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblRoles> getAll() {
        list = new ArrayList<TblRoles>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllRole", null);
            while (objResult.next()) {
                TblRoles role = new TblRoles();
                role.setRoleId(objResult.getInt("RoleId"));
                role.setRoleName(objResult.getString("RoleName"));
                role.setDescription(objResult.getString("Description"));
                role.setStatus(objResult.getBoolean("IsDelete"));
                list.add(role);
            }
            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblRoles getById(int id) {
        TblRoles role = new TblRoles();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetRoleById", params);
            while (objResult.next()) {
                role.setRoleId(objResult.getInt("RoleId"));
                role.setRoleName(objResult.getString("RoleName"));
                role.setDescription(objResult.getString("Description"));
                role.setStatus(objResult.getBoolean("IsDelete"));
            }

            return role;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
